

/**
 * 网络状态维护类
 */
const disconnectNetworkTypes = ['none','unknown','ethernet','2g', '3g']
const connectNetworkTypes = ['4g','5g','wifi']

export class Network {
	constructor(state) {
		this._initialized = false
		this._status = 'unknow'
		this.__state = state
		this._state = state
	}
	
	set state(networkType) {
		this.__state = this._state
		this._state = networkType
		// if(this.isConnected) {
		// 	this._status = 'connected'
		// } else if (this.isDisconnected) {
		// 	this._status = 'disconnected'
		// } else {
		// 	this._status = 'unknow'
		// }
	}
	
	get state() {
		return this._state
	}
	
	get online() {
		return new Promise((resolve, reject) => {
			uni.getNetworkType({
				success: function (res) {
					resolve(connectNetworkTypes.includes(res.networkType))
				}
			})
		})
	}
	
	get isConnected() {
		return disconnectNetworkTypes.includes(this.__state)  && connectNetworkTypes.includes(this._state)
	}
	
	get isDisconnected() {
		return connectNetworkTypes.includes(this.__state)  && disconnectNetworkTypes.includes(this._state)
	}
	
	initialize() {
		this._initialized = true
	}
	
	get initialized() {
		return this._initialized
	}
}